package com.qire.manhua.view.animator;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;

import com.qire.common.widget.animation.AnimationBinder;
import com.qire.manhua.R;

/**
 * 用户协议弹窗，同意协议勾选动画
 */
public class CheckAnimationBinder implements AnimationBinder<ImageView>, ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {

    private final ValueAnimator floatAnimator = ValueAnimator.ofFloat(0, 1);
    private final Runnable endRunnable;

    private ImageView view;

    /** 是否是一次性的动画播放，在离开页面后是否回收，默认都是回收，但是考虑有些页面可能只是播放动画后跳转页面还会回到这里固增加不回收的选项 */
    private boolean needDestroy = true;

    private final View.OnAttachStateChangeListener detachedFromWindowListener = new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(View v) {

        }

        @Override
        public void onViewDetachedFromWindow(View v) {
            if(needDestroy) {
                destroy();
            }
            needDestroy = true;
        }
    };

    public CheckAnimationBinder(Runnable endRunnable) {
        this.endRunnable = endRunnable;
        floatAnimator.setDuration(500);
        floatAnimator.setInterpolator(new BounceInterpolator());
        floatAnimator.setRepeatMode(ValueAnimator.RESTART);
        floatAnimator.addListener(this);
        floatAnimator.addUpdateListener(this);
    }

    /**
     * 禁用一次销毁在离开页面时
     */
    public void onceDisableDestroy() {
        this.needDestroy = false;
    }

    public void goPlay() {
        if (!floatAnimator.isStarted()) {
            floatAnimator.start();
            return;
        }
        floatAnimator.resume();
    }

    @Override
    public void bindView(ImageView view) {
        this.view = view;
        view.addOnAttachStateChangeListener(detachedFromWindowListener);
    }

    @Override
    public void destroy() {
        floatAnimator.cancel();
        if (view == null) {
            return;
        }
        view.removeOnAttachStateChangeListener(detachedFromWindowListener);
        view = null;
    }


    @Override
    public void onAnimationStart(Animator animation) {
        if(view != null) {
            view.setImageResource(R.drawable.app_user_agreement_icon_select_b);
        }
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if (view != null && endRunnable != null) {
            view.post(endRunnable);
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float fraction = animation.getAnimatedFraction();
        float scale = evaluateScale(fraction);
        rotationAction(scale);
    }

    private float evaluateScale(float fraction) {
        float coefficient = (float) Math.sin(fraction);
        float value = coefficient * 1;
        return value;
    }

    private void rotationAction(float scale) {
        if (view == null)
            return;
        view.setScaleX(scale);
        view.setScaleY(scale);
    }

}
